package cn.edu.gduf.luowowo.website.web.controller;

import cn.edu.gduf.luowowo.article.service.IDestinationService;
import cn.edu.gduf.luowowo.common.util.AjaxResult;
import cn.edu.gduf.luowowo.common.util.DateUtil;
import cn.edu.gduf.luowowo.flight.domain.FlightDetail;
import cn.edu.gduf.luowowo.flight.domain.FlightOrder;
import cn.edu.gduf.luowowo.flight.query.FlightQueryObject;
import cn.edu.gduf.luowowo.flight.service.IFlightOrderService;
import cn.edu.gduf.luowowo.flight.service.IFlightService;
import cn.edu.gduf.luowowo.member.domain.UserInfo;
import cn.edu.gduf.luowowo.website.annotation.RequiredLogin;
import cn.edu.gduf.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;

@Controller
@RequestMapping("/flight")
public class FlightController {
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IFlightService flightService;
    @Reference
    private IFlightOrderService flightOrderService;

    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("hotFlights", destinationService.queryHotFlights());
        model.addAttribute("initialA", destinationService.queryFlightsByA2E());
        model.addAttribute("initialF", destinationService.queryFlightsByF2J());
        model.addAttribute("initialK", destinationService.queryFlightsByK2P());
        model.addAttribute("initialQ", destinationService.queryFlightsByQ2W());
        model.addAttribute("initialX", destinationService.queryFlightsByX2Z());
        return "flight/index";
    }

    @RequestMapping("/search")
    public Object search(Model model, @ModelAttribute("qo") FlightQueryObject qo){
        model.addAttribute("list", flightService.query(qo));
        return "flight/searchTpl";
    }

    /**
     * 跳转到订单页面
     */
    @RequestMapping("/order")
    @RequiredLogin
    public String order(Model model, Long id, HttpServletResponse response){
        if (id == null) {
            try {
                response.sendRedirect("/flight/index");
                return "";
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //id是指哪一趟航班的id。根据航班id查询航班的具体信息
        FlightDetail air = flightService.get(id);
        model.addAttribute("air", air);
        long t = DateUtil.getDateBetween(air.getStartflytime(), air.getEndflytime());
        Long time =  Long.valueOf(t) / (60 * 60);
        model.addAttribute("hour", time);
        return "flight/order";
    }

    /**
     * 提交机票订单
     */
    @RequestMapping("/submitOrder")
    @RequiredLogin
    @ResponseBody
    public Object submitOrder(FlightOrder order, @UserParam UserInfo userInfo){
        if (userInfo == null){
            return new AjaxResult(false, "请先登录");
        }
        //生成/修改订单
        order.setUserId(userInfo.getId());
        flightOrderService.generateOrModifyOrder(order);
        return AjaxResult.SUCCESS;
    }

}
